package com.tj.test.sys.service.impl;

import com.tj.test.sys.dao.PSTMapper;
import com.tj.test.sys.dao.PersonMapper;
import com.tj.test.sys.dao.StudentMapper;
import com.tj.test.sys.dao.TeacherMapper;
import com.tj.test.sys.po.PST;
import com.tj.test.sys.service.IPSTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PSTService implements IPSTService {
    @Autowired
    private PSTMapper mapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private PersonMapper personMapper;
    @Override
    //根据账户id查询所有信息
    public Map selectAllPST(PST pst) {
        Map<String,List<PST>> map=new HashMap<>();
        List<PST> list=mapper.selectAllPST(pst);
        //查询集合是否为空
        if (list!=null){
            map.put("got",list);
        }else {
            map.put("no",list);
        }
        return map;
    }

    @Override
    //根据账户密码验证登录
    public int selectBy(String account, String pwd) {
        int temp=0;
        //查询到数据库中有该账户和密码 返回1
        temp=mapper.selectBy(account,pwd);
        if (temp==1){
            temp=1;
            return temp;
        }
        return temp;
    }

    //插入信息
    public int insertPST(PST pst) {
        int temp=0;
        //检验是否存在重复值
        temp=mapper.selectRepeat(pst);
        if (temp==0) {
            mapper.insertPST(pst);
            pst.setPersonid(mapper.selectPersonid(pst));
            //检验该信息人员的身份
            try {
            if (pst.getTeachernumber() != null&&teacherMapper.selectTeacherById(pst)==0) {
                temp = teacherMapper.insertTeacher(pst);
            } else if (pst.getStudentnumber()!= null&&studentMapper.selectStudentById(pst)==0) {
                temp = studentMapper.insertStudent(pst);
            } else { personMapper.deletePerson(pst.getPersonid());
                return -1;
            }
        }catch (Exception e){
               int rs= personMapper.deletePerson(pst.getPersonid());
               if (rs>0) {System.out.println("删除成功");}
               temp=-2;
            }
        }
        else {temp=-3;}
        return temp;
    }
}
